package com.agileai.crm.controller.procustomer;

import java.util.Date;
import java.util.List;

import com.agileai.crm.bizmoduler.procustomer.OrgInfoManage;
import com.agileai.crm.common.PrivilegeHelper;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.StandardListHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;

public class OrgInfoManageListHandler
        extends StandardListHandler {
    public OrgInfoManageListHandler() {
        super();
        this.editHandlerClazz = OrgInfoManageEditHandler.class;
        this.serviceId = buildServiceId(OrgInfoManage.class);
    }
    public ViewRenderer prepareDisplay(DataParam param){
    	User user = (User) this.getUser();
		PrivilegeHelper privilegeHelper = new PrivilegeHelper(user);
		if (privilegeHelper.isInfoCollector()) {
			setAttribute("doEdit8Save", true);
		}
		
		if (privilegeHelper.isSalesDirector() || privilegeHelper.isTelNetSalesman()){
			setAttribute("createClue", true);
			setAttribute("doDel", true);
		}
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		List<DataRow> rsList = getService().findRecords(param);
		this.setRsList(rsList);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    protected void processPageAttributes(DataParam param) {
    	initMappingItem("ORG_STATE",
                   FormSelectFactory.create("PER_STATE").getContent());
    	initMappingItem("ORG_TYPE",
	           FormSelectFactory.create("ORG_TYPE").getContent());
	   setAttribute("ORG_STATE",
	           FormSelectFactory.create("PER_STATE")
	                            .addSelectedValue(param.get("ORG_STATE")));
	   setAttribute("ORG_TYPE",
	           FormSelectFactory.create("ORG_TYPE")
	                            .addSelectedValue(param.get("ORG_TYPE")));
    }

    protected void initParameters(DataParam param) {
    	initParamItem(param, "ORG_STATE", "0");
    	initParamItem(param, "ORG_TYPE", "");
     	initParamItem(param, "ORG_CREATER_NAME", "");
     	initParamItem(param,"sdate","");
		initParamItem(param,"edate",DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL,DateUtil.getDateAdd(new Date(), DateUtil.DAY, 1)));
    }
    @PageAction
    public ViewRenderer doCreateClueAction(DataParam param) {
		storeParam(param);
		String a = "OrgInfoCreateClueEdit";
		return new DispatchRenderer(getHandlerURL(a) + "&"
				+ OperaType.KEY + "=doCreateClueAction&comeFrome=doCreateClueAction");
	}
    protected OrgInfoManage getService() {
        return (OrgInfoManage) this.lookupService(this.getServiceId());
    }
   
}
